package com.moneytree.action;

import com.moneytree.model.User;
import com.moneytree.service.UserService;
import com.moneytree.utils.ActionUtils;

public class UserAction extends ActionUtils {

	private static final long serialVersionUID = 1L;
	
	private UserService userService;
	private User user;
	
	public String index(){
		User user = (User)session.get("user");
		if(null==user) return LOGIN;
		return SUCCESS;
	}
	
	public String toRegister(){
		return SUCCESS;
	}

	public String register(){
		user.setExperiencegold(5888.00);
		userService.saveOrUpdate(user);
		session.put("user", user);
		return SUCCESS;
	}

	public String toLogin(){
		return SUCCESS;
	}

	public String login(){
		User temp = new User();
		temp.setUsername(user.getUsername());
		temp = userService.queryUser(temp);
		try {
			if(user.getPassword().equals(temp.getPassword())){
				session.put("user", temp);
				return SUCCESS;
			}else{
				throw new Exception();
			}
		} catch (Exception e) {
			return LOGIN;
		}
	}
	
	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
}
